package com.sskj.base.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.sskj.base.R;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.ClickUtil;

import java.util.List;


public class SimpleTextBottomDialog extends BottomSheetDialog {

    RecyclerView dialogList;

    private BaseAdapter<String> adapter;
    private OnItemSelectListener mListener;

    public SimpleTextBottomDialog(@NonNull Context context) {
        super(context);
        init();
    }

    public SimpleTextBottomDialog(@NonNull Context context, int theme) {
        super(context, theme);
        init();
    }

    protected SimpleTextBottomDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private void init() {
        setContentView(R.layout.simple_text_dialog);
        dialogList = findViewById(R.id.dialog_list);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.line_1_divider));
        dialogList.addItemDecoration(itemDecoration);
        dialogList.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new BaseAdapter<String>(R.layout.item_spimple_text, null, dialogList) {
            @Override
            public void bind(ViewHolder holder, String item) {
                holder.setText(R.id.item_text, item);
                ClickUtil.click(holder.getView(R.id.item_text), view -> {
                    if (mListener != null) {
                        mListener.onItemSelect(holder.getAdapterPosition(), item);
                    }
                });
            }
        };
    }

    public SimpleTextBottomDialog setData(List<String> data) {
        adapter.setNewData(data);
        this.show();
        return this;
    }


    public interface OnItemSelectListener {
        void onItemSelect(int position, String text);
    }

    public SimpleTextBottomDialog setOnItemSelectListener(OnItemSelectListener listener) {
        this.mListener = listener;
        return this;
    }
}
